/*************************************************
Author: will
Date: 3/4/2013
Description:
Notes: 
*************************************************/

#ifndef __CClanBattle_h__
#define __CClanBattle_h__

#include "CTArray.h"
#include "CTHashObjectMgr.h"
#include "commclass.h"
#include "CTRandomNum.h"
#include "../common/CRankArray.h"
#include "../battle/CBattleData.h"
#include "MsgClanBattle.pb.h"

//家族战战斗记录
class CClanBattleRecorder
{
public:
    int _iRound;            //回合数
    unsigned int _uiOppGID; //对手GID
    T_VIDEO_KEY _szVideoKey;//战斗录像ID
    bool _bIsWin;           //是否胜利
};

//家族战成员数据
class CClanBattleRole
{
public:
    CClanBattleRole()
    {

    }

    static bool IsClanVip(unsigned char ucVipLevel)
    {
        return ucVipLevel >= 7;
    }

    bool IsVip() const
    {
        return IsClanVip(_ucVipLevel);
    }

    void AddRecorder(CClanBattleRecorder& stRder, int iNowTime)
    {
        if (!CTTimeUtils::IsSameDay(iNowTime, _iRecorderTime))
        {
            _astRecorders.Clear();
        }
        
        if (_astRecorders.Size() == _astRecorders.GetCount())
        {
            _astRecorders.ShiftDelOneItem(0);
        }

        _astRecorders.AddOneItem(stRder);
        _iRecorderTime = iNowTime;
    }

public:
    unsigned int _uiGID;
    unsigned int _uiClanID;
    CTSizeString<ROLENAME_LEN> _szRoleName;
    unsigned char _ucAcademy;

    unsigned char _ucVipLevel;          //VIP 等级
    unsigned char _ucConnWinCount;      //连胜次数
    unsigned char _ucInspireCount;      //鼓舞次数
    unsigned char _ucLeftBlood;         //剩余血百分比
    bool  _bHasInspired;                //是否已经鼓舞过
    bool _bIsLoss;                      //是否已经失败

    CBattleRoleList _stBattleRoleList;  //战斗对象
    int _iMaxBlood;                     //最大血量

    //展示类数据
    unsigned char _ucCarrer;            //职业
    unsigned char _ucIsMale;            //性别
    short _shLevel;                     //等级
    CPlayerEquip _stEquip;              //装备
    unsigned int _uiSkillID;            //普通攻击技能ID
    unsigned int _uiMountID;            //坐骑ID

    //战斗记录
    int           _iRecorderTime;           //记录时间
    CTLib::CTArray<CClanBattleRecorder, CLAN_BATTLE_RECORDER_COUNT> _astRecorders;
};

class CClanBattleRoleIndex
{
public:
    CClanBattleRoleIndex()
    {

    }

public:
    unsigned int _uiGID;
    int     _iIndex;
};

//家族战斗录像
class CClanBattleVideoData
{
public:
    CClanBattleVideoData()
    {

    }

public:
    unsigned int _uiGidA;
    unsigned int _uiClanIDA;
    unsigned char _ucAOrgLeftBoold;  //战斗前剩余血量
    unsigned char _ucALeftBlood;    //剩余血量百分比
    CTSizeString<CLAN_NAME_STRING_SIZE> _szClanNameA;
    CTSizeString<ROLENAME_LEN> _szRoleNameA;
    CBattleRoleList _stBattleRoleA;

    unsigned int _uiGidB;
    unsigned int _uiClanIDB;
    unsigned char _ucBOrgLeftBoold; 
    unsigned char _ucBLeftBlood;
    CTSizeString<CLAN_NAME_STRING_SIZE> _szClanNameB;
    CTSizeString<ROLENAME_LEN> _szRoleNameB;
    CBattleRoleList _stBattleRoleB;

    int _iRoundNum;
    bool _bIsAAtkFirst;  //是否A先出手
};

class CClanBattleVideo
{
public:
    CClanBattleVideo()
    {
        Clear();
    }

    void Clear(unsigned short usRound = 0)
    {
        _usRound = usRound;
        _astBattleVideo.Clear();
    }

    int Size()
    {
        return _astBattleVideo.Size();
    }

public:
    unsigned short _usRound;        //回合数
    CTLib::CTArray<CClanBattleVideoData, MAX_CLAN_MEMBER_COUNT> _astBattleVideo;      
};

//家族战家族数据
class CBattleClan
{
public:
    CBattleClan()
    {
        _usNowRound = 0;
        _uiClanID = 0;
        _astWaitRoles.Clear();
        _astWinRoles.Clear();

        _stLastClanBattleVideo.Clear();
        _stNowClanBattleVideo.Clear();

        _usNowLevel = 0;
        _usKillCount = 0;
    }

    bool IsLoss() const
    {
        return _astWaitRoles.Size() == 0 && _astWinRoles.Size() == 0;
    }

    void CheckRound(unsigned short usNowRound);

    //是否有等待战斗的用户
    bool HasWaitUser(unsigned short usNowRound);

    //报名
    int AddBattleRole(CClanBattleRoleIndex& stRoleIndex);

    //查找对手
    int FindUser(unsigned short usNowRound, CClanBattleRoleIndex& stBattleRoleIndex);

    //战斗结果结算
    int AtkOver(CClanBattleRoleIndex& stBattleRoleIndex, bool bIsWin, CClanBattleVideoData& stBattleVideoData);

    //添加战斗结果
    int AddVideo(CClanBattleVideoData& stBattleVideoData);

    //获取战斗结果
    int GetVideo(unsigned short usRound, bool bHasFinished, ::google::protobuf::RepeatedPtrField<MsgClanBattleVideoData>& stVideoList);

public:
    unsigned int _uiClanID;
    CTSizeString<CLAN_NAME_STRING_SIZE> _szClanName;
    unsigned char _ucAcademy;
    unsigned short _usNowLevel;   //家族等级
    unsigned short _usKillCount; //总击杀数

    unsigned short _usNowRound;

    CTLib::CTArray<CClanBattleRoleIndex, MAX_CLAN_MEMBER_COUNT> _astWaitRoles;      //参赛的成员
    CTLib::CTArray<CClanBattleRoleIndex, MAX_CLAN_MEMBER_COUNT> _astWinRoles;       //胜利的成员

    CClanBattleVideo _stLastClanBattleVideo;     //上轮录像，需要展示给玩家       
    CClanBattleVideo _stNowClanBattleVideo;      //本轮录像，这轮录像
};

class CClanBattleRoleRank : public CRankArray<CClanBattleRankData, CLAN_BATTLE_ROLE_RANK_COUNT>
{
public:
    CClanBattleRoleRank()
    {

    }

    virtual bool IsSame(CClanBattleRankData&stA, CClanBattleRankData& stB) const
    {
        return stA._uiID == stB._uiID;
    }

    virtual int CmpFunc(CClanBattleRankData&stA, CClanBattleRankData& stB) const
    {
        return ((stA._iData * 1000000 + stA._iData2) - (stB._iData * 1000000 + stB._iData2));
    }
};

class CClanBattleClanRank : public CRankArray<CClanBattleRankData, CLAN_BATTLE_CLAN_RANK_COUNT>
{
public:
    CClanBattleClanRank()
    {

    }

    virtual bool IsSame(CClanBattleRankData&stA, CClanBattleRankData& stB) const
    {
        return stA._uiID == stB._uiID;
    }

    virtual int CmpFunc(CClanBattleRankData&stA, CClanBattleRankData& stB) const
    {
        return ((stA._iData * 100000 + stA._iData2) - (stB._iData * 100000 + stB._iData2));
    }
};



#endif // __CClanBattle_h__

